package com.yingsheng.bi.hks.basic.utils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpMethod;
import org.springframework.util.StringUtils;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class HttpUtils {

    private static final Logger log = LoggerFactory.getLogger(HttpUtils.class);

    private HttpUtils() {
    }

    public static Object getRequestAttribute(String name) {
        HttpServletRequest httpRequest = getHttpRequest();
        return httpRequest == null ? "" : httpRequest.getAttribute(name);
    }

    public static void setRequestAttribute(String name, String value) {
        HttpServletRequest httpRequest = getHttpRequest();
        if (httpRequest != null) {
            httpRequest.setAttribute(name, value);
        }
    }

    public static String getRequestHeader(String name) {
        HttpServletRequest httpRequest = getHttpRequest();
        return httpRequest == null ? "" : httpRequest.getHeader(name);
    }

    public static String getResponseHeader(String name) {
        HttpServletResponse response = getHttpResponse();
        if (response != null) {
            return response.getHeader(name);
        }
        return null;
    }

    public static void setResponseHeader(String name, String value) {
        HttpServletResponse response = getHttpResponse();
        if (response != null) {
            response.setHeader(name, value);
        }
    }

    public static HttpServletRequest getHttpRequest() {
        RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
        if (requestAttributes == null) {
            return null;
        } else {
            ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) requestAttributes;
            return servletRequestAttributes.getRequest();
        }
    }

    public static HttpServletResponse getHttpResponse() {
        RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
        if (requestAttributes == null) {
            return null;
        } else {
            ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) requestAttributes;
            return servletRequestAttributes.getResponse();
        }
    }

    public static String getRequestCode() {
        String requestUri = getRequestUri();
        if (StringUtils.hasText(requestUri)) {
            if (requestUri.startsWith("/")) {
                requestUri = requestUri.substring(1);
            }
            requestUri = requestUri.replaceAll("/", ".");
        }
        return requestUri;
    }

    public static String getRequestUri() {
        HttpServletRequest httpRequest = getHttpRequest();
        return httpRequest == null ? "" : httpRequest.getRequestURI();
    }

    public static boolean isFormPost(HttpServletRequest request) {
        String contentType = request.getContentType();
        return contentType != null && contentType.contains("application/x-www-form-urlencoded") && HttpMethod.POST.matches(request.getMethod());
    }

}

